package org.softmed.rest.admin.resource;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import org.softmed.rest.admin.DetailChooser;
import org.softmed.rest.admin.DetailLevelListener;
import org.softmed.rest.admin.DetailChooser.DetailLevel;
import org.softmed.rest.admin.interfaces.EventListener;
import org.softmed.rest.admin.interfaces.FileHandler;
import org.softmed.rest.admin.interfaces.RemoveListener;
import org.softmed.swing.IconManager;

public class ResourceActionPanel extends JPanel implements ActionListener {

	EventListener addResourceListener;
	RemoveListener removeListener;

	int width = 20;
	int height = 20;

	JLabel msg = new JLabel();

	JLabel validateError = new JLabel(IconManager.warning); // OK
	JLabel changed = new JLabel(IconManager.changed); // OK
	JButton retrieve = new JButton(IconManager.leftArrow);
	JButton updateOnServer = new JButton(IconManager.updateToServer);

	// JButton validateConfig = new JButton( IconManager.validate); // OK
	JButton validateFiles = new JButton(IconManager.validate); // OK

	// JToggleButton single = new JToggleButton("Single"); // OK
	// JToggleButton detailed = new JToggleButton("Detailed"); // OK
	DetailChooser detailChooser = new DetailChooser(true, DetailLevel.RESOURCE,
			DetailLevel.HANDLER, DetailLevel.METHODS, DetailLevel.MEDIA_TYPE);

	FileHandler fileHandler;
	private ButtonGroup gb;

	public ResourceActionPanel() {
		// setBackground(Color.BLACK);
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

		setPreferredSize(new Dimension(540, 30));
		setMaximumSize(new Dimension(540, 30));
		setMinimumSize(new Dimension(540, 30));

		addButton(retrieve);
		// addButton(validateFiles);
		addButton(updateOnServer);

		// addButton(validateConfig);
		add(detailChooser);
		add(Box.createHorizontalGlue());

		add(validateError);
		// add(changed);
		validateError.setVisible(false);

		updateOnServer
				.setToolTipText("Forces the resource to be updated on the server, changing its REST configuration");

		// validateConfig
		// .setToolTipText("Validates current resource configuration XML file");
		validateFiles
				.setToolTipText("Validates current resource configuration XML file and checks for the existence of every refered file on the server");

		validateError.setToolTipText("There was an error validating the file.");
		changed
				.setToolTipText("The configuration has changed from the currently stored version.");

	}

	protected void addButton(AbstractButton button) {
		resizeButton(button, width, height);
		button.addActionListener(this);
		button.setEnabled(false);
		add(button);
	}

	protected void resizeButton(AbstractButton button, int width, int height) {
		button.setPreferredSize(new Dimension(width, height));
		button.setMaximumSize(new Dimension(width, height));
		button.setMinimumSize(new Dimension(width, height));
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object source = arg0.getSource();

		if (source == retrieve) {
			fileHandler.retrieveFilePath();
		}
		// else if (source == remove) {
		// removeListener.remove(this);
		// }

	}

	public EventListener getAddResourceListener() {
		return addResourceListener;
	}

	public void setAddResourceListener(EventListener addResourceListener) {
		this.addResourceListener = addResourceListener;
	}

	public RemoveListener getRemoveListener() {
		return removeListener;
	}

	public void setRemoveListener(RemoveListener removeListener) {
		this.removeListener = removeListener;
	}

	public FileHandler getFileHandler() {
		return fileHandler;
	}

	public void setFileHandler(FileHandler fileHandler) {
		this.fileHandler = fileHandler;
	}

	public void setDetailListener(DetailLevelListener listener) {
		detailChooser.setDetailListener(listener);
	}

	public void setDetailLevel(DetailLevel detailLevel) {
		detailChooser.setDetailLevel(detailLevel);
	}

}
